package com.sky.controller.admin;

import ch.qos.logback.core.status.StatusUtil;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.DishInSetmealException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    @Autowired
    RedisTemplate redisTemplate;

    /*
    * 新增菜品
    * */
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result save(@RequestBody DishDTO dishDTO){
        dishService.saveWithFavour(dishDTO);
        return Result.success();
    }

    /*
    * 菜品分页查询
    * */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询参数:{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /*
    * 删除和批量删除菜品
    * */
    @ApiOperation("菜品批量删除")
    @DeleteMapping
    //批量删除会很麻烦，所以全删
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result delete(long[] ids){
        log.info("根据id删除菜品:{}",ids);
        boolean flag = dishService.findStatus(ids);
        //起售中的菜品不能删除
        if(!flag){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //关联了套餐的菜品不能删除
        List<SetmealDish> list = dishService.findConnection(ids);
        if(list.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        dishService.delete(ids);
        return Result.success();
    }

    @ApiOperation("菜品数据回想")
    @GetMapping("/{id}")
    public Result<DishVO> select(@PathVariable Long id){
        log.info("回想这个id的数据：{}",id);
        DishVO dishVO = dishService.select(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    //修改菜品分类时，涉及到两个分类id，所以选择全删
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.update(dishDTO);
        return Result.success();
    }


    /*
    * 修改菜品起售停售
    * */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result modifyStatus(@PathVariable int status, long id){
        //修改菜品状态
        dishService.modifyStatus(status,id);

        //如果是停售菜品，就要停售相关套餐
        if(status == StatusConstant.DISABLE) {
            //判断当前菜品是否有套餐使用,有就返回对应的关系集合
            long[] ids = {id};
            List<SetmealDish> list = dishService.findConnection(ids);
            for (SetmealDish setmealDish : list) {
                Long setmealId = setmealDish.getSetmealId();
                setmealService.modifyStatus(StatusConstant.DISABLE,setmealId);
            }
        }
        return Result.success();
    }

    /*
    * 根据分类查询菜品，为套餐服务
    * */
    @GetMapping("/list")
    @ApiOperation("根据分类查询菜品")
    public Result findByCategoryId(Integer categoryId){
        List<Dish> list = dishService.findByCategoryId(categoryId);
        return Result.success(list);
    }
}
